import { IOutputDataItem, IDataItem } from './App';
import * as apiType from './data';

export function getData (): Promise<IOutputDataItem[]> {
    const tmpData = [
        {
          dimension: 't1',
          data: [
            {
              year: "1991",
              value: 3,
            },
            {
              year: "1992",
              value: 3,
            },
            {
              year: "1993",
              value: 3,
            },
            {
              year: "1994",
              value: 3,
            },
            {
              year: "1995",
              value: 3,
            },
            {
              year: "1996",
              value: 3,
            },
          ],
        },
        {
          dimension: 't2',
          data: [
            {
              year: "1991",
              value: 10,
            },
            {
              year: "1992",
              value: 3,
            },
            {
              year: "1993",
              value: 2,
            },
            {
              year: "1994",
              value: 1,
            },
            {
              year: "1995",
              value: 2,
            },
            {
              year: "1996",
              value: 3,
            },
          ],
        },    
    ];
    return new Promise<IDataItem[]>((resolve) => {
        setTimeout(() => {
            resolve(tmpData.map(d => {
                return {
                    ...d,
                    data: d.data.map(dd => ({ ...dd, value: Math.floor(Math.random() * 6 + 4) })),
                };
            }));
        }, 1000);
    })
    .then(res => {
        return res
            .map(d => d.data.map(dd => {
                return {
                    ...dd,
                    dimension: d.dimension,
                };
            }))
            .flat()
    });
}

export function getData1 (): Promise<apiType.IChartData[]> {
    const tmpData: IDataItem[] = [
        {
          dimension: 't1',
          data: [
            {
              year: "1991",
              value: 3,
              // 把value干掉
              // 加一个t1, t2
            },
            {
              year: "1992",
              value: 3,
            },
            {
              year: "1993",
              value: 3,
            },
            {
              year: "1994",
              value: 3,
            },
            {
              year: "1995",
              value: 3,
            },
            {
              year: "1996",
              value: 3,
            },
          ],
        },
        {
          dimension: 't2',
          data: [
            {
              year: "1991",
              value: 10,
            },
            {
              year: "1992",
              value: 3,
            },
            {
              year: "1993",
              value: 2,
            },
            {
              year: "1994",
              value: 1,
            },
            {
              year: "1995",
              value: 2,
            },
            {
              year: "1996",
              value: 3,
            },
          ],
        },    
    ];
    return new Promise<IDataItem[]>((resolve) => {
        setTimeout(() => {
            resolve(tmpData);
        }, 1000);
    })
    .then((res) => {
       return res[0].data.map((d, i) => {
           return {
               year: d.year,
               t1: tmpData[0].data[i].value,
               t2: tmpData[1].data[i].value,
           };
       });
    });
}

export function getScatterData (): Promise<apiType.IScatterData[]> {
    return fetch('https://gw.alipayobjects.com/os/antfincdn/t81X1wXdoj/scatter-data.json')
        .then(res => res.json())
}

export function getSimpleData (): Promise<apiType.ISimpleData[]> {
    return new Promise((resolve) => {
        resolve([
            { year: '1991', value: 3 },
            { year: '1992', value: 4 },
            { year: '1993', value: 3.5 },
            { year: '1994', value: 5 },
            { year: '1995', value: 4.9 },
            { year: '1996', value: 6 },
            { year: '1997', value: 7 },
            { year: '1998', value: 9 },
            { year: '1999', value: 13 },
        ]);
    });
}
